﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StreetlightGames.XnaEngine;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using StreetlightGames.SpaceShooter.Patterns;
using Microsoft.Xna.Framework.Audio;

namespace StreetlightGames.SpaceShooter.Enemies
{
    public class MotherShip : EnemyShip
    {
        private bool _isTractorBeamComplete;
        private double _tractorBeamElapsedTime;
        private const double TRACTOR_BEAM_DURATION = 3;

        private Model _model1;
        private Model _model2;
        private ModelObject _tractorBeam;
        private ModelObject _capturedShip;
        private Cue _tractorBeamCue;

        /// <summary>Gets the IsHit value</summary>
        public bool IsHit
        {
            get { return _isHit; }
        }
        private bool _isHit;

        /// <summary>Gets or sets the IsCapturingShip value</summary>
        public bool IsCapturingShip
        {
            get { return _isCapturingShip; }
            set { _isCapturingShip = value; }
        }
        private bool _isCapturingShip;

        /// <summary>Gets the IsShipCaptured value</summary>
        public bool IsShipCaptured
        {
            get { return _isShipCaptured; }
        }
        private bool _isShipCaptured;

        /// <summary>Gets the IsTractorBeamActive value</summary>
        public bool IsTractorBeamActive
        {
            get { return _tractorBeam.Visible; }
        }

        public MotherShip(Game game)
            : base(game)
        {
            ModelName = "Models\\GalagaMotherShip1";

            _model2 = Game.Content.Load<Model>("Models\\GalagaMotherShip2");
            
            _tractorBeam = new ModelObject(Game, "Models\\GalagaTractorBeam");
            _tractorBeam.Camera = SpaceShooterGame.Camera;
            _tractorBeam.Visible = false;
            _tractorBeam.Scale = 10;
            _tractorBeam.RotateY(MathHelper.Pi);
            Game.Components.Add(_tractorBeam);

            _capturedShip = new ModelObject(game, "Models\\GalagaShip");
            _capturedShip.Camera = SpaceShooterGame.Camera;
            _capturedShip.Scale = 5;
            Game.Components.Add(_capturedShip);
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            _model1 = Model;
        }

        public override void Start()
        {
            Model = _model1;
            _isTractorBeamComplete = false;
            _isCapturingShip = false;
            _isHit = false;
            _isShipCaptured = false;
            _tractorBeamElapsedTime = 0;
            _capturedShip.IsAlive = false;
            base.Start();
        }

        public override void Update(GameTime gameTime)
        {
            if (!IsTractorBeamActive)
            {
                base.Update(gameTime);
                if (!_isTractorBeamComplete && Pattern is Attack && Wave.MainMotherShip == null)
                {
                    if (Position.Z > -200)
                    {
                        StartTractorBeam();
                    }
                }
            }
            else
            {
                Rotation = new Vector3(0, MathHelper.Pi, 0);
                _tractorBeamElapsedTime += gameTime.ElapsedGameTime.TotalSeconds;
                _tractorBeam.Position = Position;
                if (_tractorBeamElapsedTime > TRACTOR_BEAM_DURATION && !_isCapturingShip)
                {
                    EndTractorBeam();
                }
            }
            if (_isShipCaptured)
            {
                _capturedShip.Position = Position + new Vector3(0, 0, -100);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (IsTractorBeamActive)
            {
                _tractorBeam.Draw(gameTime);
            }
        }

        private void StartTractorBeam()
        {
            _tractorBeamCue = AudioController.Instance.GetCue("GalagaTractorBeam");
            _tractorBeamCue.Play();
            _tractorBeam.Visible = true;
            _tractorBeamElapsedTime = 0;
            Wave.MainMotherShip = this;
        }

        private void EndTractorBeam()
        {
            _tractorBeam.Visible = false;
            _isTractorBeamComplete = true;
            Pattern.Start();
            _tractorBeamCue.Stop(AudioStopOptions.Immediate);
        }

        public void Capture()
        {
            SpaceShooterGame.CapturePlayerShip();
            _isShipCaptured = true;
            _capturedShip.IsAlive = true;
            _tractorBeamElapsedTime = 0;
            EndTractorBeam();
            AudioController.Instance.PlayCue("GalagaRetrieveShip");
            Pattern nextPattern = Pattern;
            Pattern = new MoveToFormation();
            Pattern.NextPattern = new Hover();
            Pattern.NextPattern.NextPattern = nextPattern;
        }

        internal override void Explode()
        {
            if (_isHit)
            {
                base.Explode();
                if (_isShipCaptured)
                {
                    _isShipCaptured = false;
                    _capturedShip.IsAlive = false;
                }
            }
            else
            {
                _isHit = true;
                Model = _model2;
            }
        }
    }
}
